package com.dayatang.android.Activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.TextAppearanceSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.dayatang.android.Model.SubProject;

public class SubProgressActivity extends BaseActivity {
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_progress_activity);
		
		InitProgress();
	}
	
	private void InitProgress(){
		
		SubProject project = getCurSubproject();
		
		try {
			JSONArray jsonArray = new JSONArray(project.getProgressInfo());
			
			for(int i=0;i<jsonArray.length();i++){
				
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				
				InitTableLayout(jsonObject.getString("workStepName"),jsonObject.getString("responsible"),jsonObject.getString("progressStatus"));
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
	
	private void InitTableLayout(Object... params){
		
		TableLayout table = (TableLayout) findViewById(R.id.progress_info_tablelayout);
		
		TextAppearanceSpan textappearancespan = new TextAppearanceSpan(this, R.style.normalText);   

		TableRow row = new TableRow(this); 
		row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		TextView t = new TextView(this);
		t.setGravity(Gravity.CENTER_HORIZONTAL);
		SpannableStringBuilder style=new SpannableStringBuilder((CharSequence) params[0]);
		style.setSpan(textappearancespan,0,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t.setText(style);
		
		row.addView(t);

		TextView t2 = new TextView(this);
		t2.setGravity(Gravity.CENTER_HORIZONTAL);
		SpannableStringBuilder style2=new SpannableStringBuilder((CharSequence) params[1]);
		style2.setSpan(textappearancespan,0,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t2.setText(style2);
		
		row.addView(t2);
		
		TextView t3 = new TextView(this);
		t3.setGravity(Gravity.CENTER_HORIZONTAL);
		SpannableStringBuilder style3=new SpannableStringBuilder((CharSequence) params[2]);
		style3.setSpan(textappearancespan,0,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t3.setText(style3);
		
		row.addView(t3);
		
		table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}	
